#pragma once

#include <stdio.h>
#include <iostream>
#include <mutex>
#include <sys/time.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

#define MAX_SIZE (2*1024*1024)  //2M
#define MAX_PATH 256


enum LOGLEVEL
{
	LOG_LEVEL_NONE,
	LOG_LEVEL_ERROR,     // error
	LOG_LEVEL_WARNING,   // warning
	LOG_LEVEL_DEBUG,     // debug
	LOG_LEVEL_INFO,      // info	
};

enum LOGTARGET
{
	LOG_TARGET_NONE      = 0x00,
	LOG_TARGET_CONSOLE   = 0x01,
	LOG_TARGET_FILE      = 0x10
};

union semun
{
    /* data */
    int val;
    struct semid_ds *buf;
    unsigned short *array;
    struct seminfo *_buf;
};


class Log
{
public:
    static Log* getInstance();

    int createFile();
    void init(LOGLEVEL level, LOGTARGET target);
    void uinit();

    void setLogLevel(LOGLEVEL level);
    LOGLEVEL getLogLevel();

    void setLogTarget(LOGTARGET target);
    LOGTARGET getLogTarget();

    static int writeLog(
    LOGLEVEL loglevel,         // Log等级
    const char* fileName,   // 函数所在的文件名
    const char* function,   // 函数名
    int lineNumber,            // 行号
    char* format,              // 格式
    ...);    

    static void outputToTarget();
private:

    Log();
    ~Log();


    static Log* mLog;

    // //互斥锁
    static std::mutex mMutexLog;

    //log buffer
    static std::string mLogBuffer;
    //已经写的log长度
    static int mWrittenSize;

    //文件句柄
    static int mFileFd;

    //log等级
    LOGLEVEL mLogLevel;

    //Log输出位置
    LOGTARGET mLogTarget;

};